package com.eclipse.e0112_多线程.聊天;

/*
 * 抽象方法 absdtract void doGet(Reader in,Writer out){}
 * 
 * 参数wr
 */
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server {
	private static Scanner scanner = new Scanner(System.in);
	private static int port = 8080;

	public static void main(String[] args) throws IOException {

		ServerSocket serverSocket = new ServerSocket(port);
		Socket socket = serverSocket.accept();

		InputStream inputStream = socket.getInputStream();
		OutputStream outputStream = socket.getOutputStream();

		BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(outputStream));
		ServerMesh sm = new ServerMesh(in, out);
		sm.start();
	}
}




abstract class Servlet {
	abstract void doGet(BufferedReader in, BufferedWriter out);
}





class AServlet extends Servlet {
	private BufferedReader in;
	private BufferedWriter out;
	AServlet(BufferedReader in, BufferedWriter out) {
		this.out = out;
		this.in = in;
	}
	void doGet(BufferedReader in, BufferedWriter out) {
		try {
			out.write("我是AServlet\r\n");
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}









class BServlet extends Servlet {
	private BufferedReader in;
	private BufferedWriter out;
	BServlet(BufferedReader in, BufferedWriter out) {
		this.in = in;
		this.out = out;
	}
	void doGet(BufferedReader in, BufferedWriter out) {
		try {
			out.write("我是BServlet\r\n");
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}







class ServerMesh extends Thread {
	private BufferedReader in;
	private BufferedWriter out;
	ServerMesh(BufferedReader in, BufferedWriter out) {
		this.in = in;
		this.out = out;
	}
	public void run() {
		String info = null;
		while (true) {
			try {
				info = in.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (info.equals("a")) {
				AServlet aServlet = new AServlet(in, out);
				aServlet.doGet(in, out);
			}
			if (info.equals("b")) {
				BServlet bServlet = new BServlet(in, out);
				bServlet.doGet(in, out);
			}
		}
	}
}